huffman编码实现 w = {2,3,5,7,11,13,17,19, 23,29,31,37,41}谢谢了···
来源:百度知道 编辑:UC知道 时间:2024/09/12 22:04:04
41:111
37:110
31:100
29:010
23:001
19:000
17:1010
13:0111
11:0110
7:10110
5:101110
3:1011111
2:1011110
Huffman编码结果不唯一哦!
下面附上参考程序哈:
void Error(char *message) //错误提示
{
system("cls"); //清屏
fprintf(stderr,"Error:%s\n",message); //显示错误信息
exit(1);
}
HuffmanCode HuffmanCoding(HuffmanTree HT,HuffmanCode HC,unsigned int *w,unsigned int n) //Huffman编码函数,先构建Huffman树
{
unsigned int i,s1=0,s2=0;
HuffmanTree p;
char *cd;
unsigned int f,c,start,m;
MinCode min;
if(n<=1) Error("Code too small!"); //如果权值只有一个,则显示错误信息"code too small!"
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT,i=0;i<=n;i++,p++,w++)
{
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for(;i<=m;i++,p++)
{ <